package design.pattern.Lu19VisitorPattern;

import lombok.Data;

/**
 * @program JavaBase
 * @description: 访问者模式
 *  一个报表Visitor 就是一种格式，需要的时候，实例一个出来，如果需要更改格式，可以在建立一个报表模版
 * @author: zhanglu
 * @create: 2020-05-07 16:24:00
 */
public class VisitorPattern02 {

    public static void main(String[] args) {
        CommonEmployee02 commonEmployee02 = new CommonEmployee02();
        commonEmployee02.setName("张三");
        commonEmployee02.setSalary("1000");
        commonEmployee02.setJob("拧螺丝，准备造火箭");
        commonEmployee02.accept(new Visitor());

        Manager02 manager02 = new Manager02();
        manager02.setName("里斯");
        manager02.setSalary("10000");
        manager02.setPerformance("拿下xxx客户");
        manager02.accept(new Visitor());
    }

}

interface IVisitor{
    <T> void visit(T t);
}

/**
 * 一个报表
 */
class Visitor implements IVisitor{

    @Override
    public <T> void visit(T t) {
        if(t instanceof CommonEmployee02){
            System.out.println(commonEmployee((CommonEmployee02) t));
        }else {
            System.out.println(managerEmployee((Manager02) t));
        }
    }

    private String commonEmployee(CommonEmployee02 commonEmployee02){
        String info = this.getBasicInfo(commonEmployee02);
        info += "\t工作：" + commonEmployee02.getJob();
        return info;
    }

    private String managerEmployee(Manager02 manager02){
        String info = this.getBasicInfo(manager02);
        info += "\t业绩：" + manager02.getPerformance();
        return info;
    }

    private String getBasicInfo(Employee02 employee02){
        String info = "姓名：" + employee02.getName() + "\t薪资：" + employee02.getSalary();
        return info;
    }

}
@Data
abstract class Employee02{
    private String name;
    private String salary;

    protected abstract void accept(IVisitor iVisitor);
}
@Data
class CommonEmployee02 extends Employee02{

    private String job;

    @Override
    protected void accept(IVisitor iVisitor) {
        iVisitor.visit(this);
    }
}
@Data
class Manager02 extends Employee02{

    private String performance;

    @Override
    protected void accept(IVisitor iVisitor) {
        iVisitor.visit(this);
    }
}


